package com.immomo.test.common;

import java.io.*;
import java.util.Properties;

public class FileUtil {
    public static void renameTo(File oldFile, File newFile) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            is = new FileInputStream(oldFile);
            os = new FileOutputStream(newFile);
            int len;
            byte[] buffer = new byte[2048];
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            oldFile.delete();
        } finally {
            try {
                is.close();
                os.close();
            } catch (Exception r) {

            }
        }
    }

    public static float getFileSize(File file){
        if(file.exists() && file.isFile()){
            long fileS = file.length();
            long intSize = fileS * 100 / 1024;
            return intSize / 100f;
        }
        return 0;
    }




    public static String loadAPiKey() {
        File file = new File(System.getProperty("user.home") + "/.gradle/gradle.properties");
        if (!file.exists()) {
            return "";
        }
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = new FileInputStream(file);
            Properties prop = new Properties();
            try {
                prop.load(resourceAsStream);
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
            return prop.getProperty("idea.tinypng.key", "");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resourceAsStream != null) {
                try {
                    resourceAsStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    public static boolean loadCanDetectFileChange() {
        File file = new File(System.getProperty("user.home") + "/.gradle/gradle.properties");
        if (!file.exists()) {
            return true;
        }
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = new FileInputStream(file);
            Properties prop = new Properties();
            try {
                prop.load(resourceAsStream);
            } catch (IOException e) {
                e.printStackTrace();
                return true;
            }
            String result = prop.getProperty("idea.tinypng.detect.change", "1");
            if ("0".equals(result)) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resourceAsStream != null) {
                try {
                    resourceAsStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
}
